package com.aspyn.fm;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FileMakerActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	Button btnCreate;
	Button btnDisplay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btnCreate = (Button)findViewById(R.id.btnCreate);
		btnCreate.setOnClickListener(this);
		btnCreate.setText("Create File");

		btnDisplay = (Button)findViewById(R.id.btnReadFile);
		btnDisplay.setOnClickListener(this);
		btnDisplay.setText("Display File Contents");

		//Creates/Shows alert
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("READ ME!");
		alert.setMessage("Happy birthday!");
		alert.setPositiveButton("Thank you", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "You are nice", Toast.LENGTH_SHORT).show();
			}
		});
		alert.setNeutralButton("It's not by birthday", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "You are so-so", Toast.LENGTH_SHORT).show();
			}
		});
		alert.setNegativeButton("STFU", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "You are mean", Toast.LENGTH_SHORT).show();
			}
		});
		alert.show();
		
		WebView wv = (WebView)findViewById(R.id.webView);
		wv.loadUrl("http://www.facebook.com");

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

		EditText txtView = (EditText)findViewById(R.id.txtFileName);//file name
		EditText txtViewFileMsg = (EditText)findViewById(R.id.txtFileText);//file msg

		if (v.equals(btnCreate)) {
			//Create a file

			try {
				OutputStreamWriter out = new OutputStreamWriter(openFileOutput(txtView.getText().toString(), Context.MODE_WORLD_READABLE));//creates file with name txtView.getText()
				out.write(txtViewFileMsg.getText().toString());//writes file
				out.close();

				Toast.makeText(this, "Create", Toast.LENGTH_SHORT).show();

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (v.equals(btnDisplay)) {
			try {
				InputStream in = openFileInput(txtView.getText().toString());//opens file
				if (in != null) {
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));//Opens created buffer to read file
					String str;
					while ((str = reader.readLine()) != null) {//reads lines of created file
						Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mymenu, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.item1:
			Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item2:
			Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item3:
			Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item4:
			Toast.makeText(this, "4", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item5:
			Toast.makeText(this, "5", Toast.LENGTH_SHORT).show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}